//
//  SSSGuiedViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/4/3.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSGuiedViewController.h"
#import "UIView+XHAdd.h"
#import "SSSHomeViewController.h"
#import "SSSPersonCenterController.h"
#import "SSSMyIncomeViewController.h"
#import "SSSLoginViewController.h"
@interface SSSGuiedViewController ()<UIScrollViewDelegate>
{
    BOOL isGetin; // 是否再次滑动进入app
    BOOL isHidden;
}

@property (nonatomic, strong) NSArray       *imageArray;
@property (nonatomic, strong) UIPageControl *imagePageControl;
/** scrollView */
@property (nonatomic,strong)  UIScrollView *guidePageView;
@property (nonatomic, strong) UIButton *skipButton;

@end

@implementation SSSGuiedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isHidden = YES;
    NSArray *imageArray = @[[UIImage imageNamed:@"pic_Guide_one"],
                            [UIImage imageNamed:@"pic_Guide_two"],
                            [UIImage imageNamed:@"pic_Guide_three"]];
    self.imageArray = imageArray;
    _guidePageView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    [_guidePageView setBackgroundColor:[UIColor colorWithRed:0.2275 green:0.6118 blue:0.702 alpha:1.0]];
    [_guidePageView setContentSize:CGSizeMake(KSCREEN_Width*self.imageArray.count, KSCREEN_Height)];
    [_guidePageView setBounces:NO];
    [_guidePageView setPagingEnabled:YES];
    [_guidePageView setShowsHorizontalScrollIndicator:NO];
    [_guidePageView setDelegate:self];
    [self.view addSubview:_guidePageView];
    // 设置引导页上的跳过按钮
    _skipButton = [[UIButton alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.8, KSCREEN_Height*0.15, 50, 25)];
    [_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    [_skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_skipButton setBackgroundColor:[UIColor colorWithHex:@"aaadaf"]];
    [_skipButton.layer setCornerRadius:5.0];
    [_skipButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_skipButton];
    
    
    // 添加在引导视图上的多张引导图片
    for (int i=0; i<self.imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KSCREEN_Width*i, 0, KSCREEN_Width, KSCREEN_Height)];
        imageView.image = self.imageArray[i];
        [_guidePageView addSubview:imageView];
        
        // 设置在最后一张图片上显示进入体验按钮
        if (i == self.imageArray.count-1 && isHidden == YES) {
            [imageView setUserInteractionEnabled:YES];
            
            UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.3, KSCREEN_Height*0.85, 156, 35)];
            startButton.centerX = self.view.centerX;
            [startButton setTitle:@"立即体验" forState:UIControlStateNormal];
            [startButton setTitleColor:[UIColor colorWithRed:164/255.0 green:201/255.0 blue:67/255.0 alpha:1.0] forState:UIControlStateNormal];
            [startButton.titleLabel setFont:[UIFont systemFontOfSize:21]];
            [startButton setTitle:@"立即体验" forState:UIControlStateNormal];
            //                [startButton setBackgroundImage:[UIImage imageNamed:@"立即体验"] forState:UIControlStateNormal];
            [startButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:startButton];
        }
    }
    
    // 设置引导页上的页面控制器
    self.imagePageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.0, KSCREEN_Height*0.83, KSCREEN_Width*1.0, KSCREEN_Height*0.01)];
    self.imagePageControl.currentPage = 0;
    self.imagePageControl.numberOfPages = self.imageArray.count;
    self.imagePageControl.pageIndicatorTintColor = ZDCommonBgColor;
    self.imagePageControl.currentPageIndicatorTintColor = ZDCommonBlueColor;
    [self.view addSubview:self.imagePageControl];

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview {
    int page = scrollview.contentOffset.x / scrollview.frame.size.width;
    [self.imagePageControl setCurrentPage:page];
    if (self.imageArray && page == self.imageArray.count-1 && isGetin == YES) {
        [self buttonClick:nil];
        isGetin = NO;
        return;
    }
    if (self.imageArray && page == self.imageArray.count-1) {
        isGetin = YES;
    }else{
        isGetin = NO;
    }
}

- (void)buttonClick:(UIButton *)button {
    //[UIView animateWithDuration:DDHidden_TIME animations:^{
    //self.view.alpha = 0;
    //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DDHidden_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //            [self performSelector:@selector(removeGuidePageHUD) withObject:nil afterDelay:1];
    //
    //        });
    
    //[self performSelector:@selector(removeGuidePageHUD) withObject:nil afterDelay:1];
    
    //self.view.window.rootViewController = [[QMUITabBarViewController alloc]init];
    //[self exhangeRootController];
    self.view.window.rootViewController = [[SSSLoginViewController alloc]init];
    [self.view removeFromSuperview];
    
    // }];
}

- (void)removeGuidePageHUD {
    self.view.window.rootViewController = [[SSSLoginViewController alloc]init];
    //self.view.window.rootViewController = [[QMUITabBarViewController alloc]init];
    [self.view removeFromSuperview];
    
}
#pragma mark - 切换跟控制器
- (void)exhangeRootController{
    
    
    SSSHomeViewController *home=[SSSHomeViewController new];
    home.view.backgroundColor = [UIColor redColor];
    QMUINavigationController *homeNav= [[QMUINavigationController alloc]
                                        initWithRootViewController:home];
    
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_06"];
    
    UIViewController *inCome = [SSSMyIncomeViewController new];
    inCome.view.backgroundColor = [UIColor blueColor];
    QMUINavigationController *inComeNav = [[QMUINavigationController alloc]
                                           initWithRootViewController:inCome];
    inComeNav.tabBarItem.title = @"我的收益";
    inComeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_07"];
    
    SSSPersonCenterController *personal = [SSSPersonCenterController new];
    //personal.view.backgroundColor = [UIColor brownColor];
    QMUINavigationController *personalNav = [[QMUINavigationController alloc]
                                             initWithRootViewController:personal];
    personalNav.tabBarItem.title = @"个人中心";
    personalNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_08"];
    
    QMUITabBarViewController *tabVc = [QMUITabBarViewController new];
    tabVc.viewControllers = @[homeNav, inComeNav, personalNav];
    tabVc.tabBar.translucent = false;
    
    self.view.window.rootViewController = tabVc;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
